package com.ruoyi.wms.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsCustomerMapper;
import com.ruoyi.wms.domain.WmsCustomer;
import com.ruoyi.wms.service.IWmsCustomerService;

/**
 * 客户信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class WmsCustomerServiceImpl implements IWmsCustomerService 
{
    @Autowired
    private WmsCustomerMapper wmsCustomerMapper;

    /**
     * 查询客户信息
     * 
     * @param id 客户信息主键
     * @return 客户信息
     */
    @Override
    public WmsCustomer selectWmsCustomerById(Long id)
    {
        return wmsCustomerMapper.selectWmsCustomerById(id);
    }

    /**
     * 查询客户信息列表
     * 
     * @param wmsCustomer 客户信息
     * @return 客户信息
     */
    @Override
    public List<WmsCustomer> selectWmsCustomerList(WmsCustomer wmsCustomer)
    {
        // 设置租户ID
        wmsCustomer.setTenantId(SecurityUtils.getTenantId());
        return wmsCustomerMapper.selectWmsCustomerList(wmsCustomer);
    }

    /**
     * 新增客户信息
     * 
     * @param wmsCustomer 客户信息
     * @return 结果
     */
    @Override
    public int insertWmsCustomer(WmsCustomer wmsCustomer)
    {
        wmsCustomer.setTenantId(SecurityUtils.getTenantId());
        wmsCustomer.setCreateBy(SecurityUtils.getUsername());
        wmsCustomer.setCreateTime(DateUtils.getNowDate());
        return wmsCustomerMapper.insertWmsCustomer(wmsCustomer);
    }

    /**
     * 修改客户信息
     * 
     * @param wmsCustomer 客户信息
     * @return 结果
     */
    @Override
    public int updateWmsCustomer(WmsCustomer wmsCustomer)
    {
        wmsCustomer.setUpdateBy(SecurityUtils.getUsername());
        wmsCustomer.setUpdateTime(DateUtils.getNowDate());
        return wmsCustomerMapper.updateWmsCustomer(wmsCustomer);
    }

    /**
     * 批量删除客户信息
     * 
     * @param ids 需要删除的客户信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsCustomerByIds(Long[] ids)
    {
        return wmsCustomerMapper.deleteWmsCustomerByIds(ids);
    }

    /**
     * 删除客户信息信息
     * 
     * @param id 客户信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsCustomerById(Long id)
    {
        return wmsCustomerMapper.deleteWmsCustomerById(id);
    }

    /**
     * 根据客户编码查询客户信息
     * 
     * @param customerCode 客户编码
     * @return 客户信息
     */
    @Override
    public WmsCustomer selectWmsCustomerByCode(String customerCode)
    {
        return wmsCustomerMapper.selectWmsCustomerByCode(customerCode, SecurityUtils.getTenantId());
    }

    /**
     * 校验客户编码是否唯一
     * 
     * @param wmsCustomer 客户信息
     * @return 结果
     */
    @Override
    public boolean checkCustomerCodeUnique(WmsCustomer wmsCustomer)
    {
        Long customerId = StringUtils.isNull(wmsCustomer.getId()) ? -1L : wmsCustomer.getId();
        WmsCustomer info = wmsCustomerMapper.selectWmsCustomerByCode(wmsCustomer.getCustomerCode(), SecurityUtils.getTenantId());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != customerId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}